package com.southpark.demo.control.proxies
{
	import com.southpark.framework.control.proxies.AbstractProxy;
	import com.southpark.framework.control.proxies.IDeserializer;
	import com.southpark.framework.control.responders.IResponder;
	import com.southpark.system.interfaces.application.IApplication;
	import com.southpark.system.interfaces.managers.IApplicationManager;
	
	import flash.utils.getQualifiedClassName;
	
	import mx.core.FlexGlobals;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	
	public class BaseProxy extends AbstractProxy
	{
		[Bindable]public static var proxyIndex:int;
		
		static protected var isDebugMode:Boolean;
		
		protected var startTime:Date;
		private var applicationManager:IApplicationManager = IApplication(FlexGlobals.topLevelApplication).applicationManager;
		
		
		public function BaseProxy(responder:IResponder,serviceClass:Class,fakeServiceClass:Class=null)
		{
			super(responder,serviceClass,fakeServiceClass);
		}
		
		override protected function callService(deserializer:IDeserializer,backendMethod:String=null,parameters:Array=null):void
		{						
			if (isDebugMode)
			{
				proxyIndex++;
				startTime = new Date();
			};
			
			super.callService(deserializer,backendMethod,parameters);
		}
		
		override public function onServiceSuccess(event:ResultEvent):void
		{
			if (isDebugMode && startTime)
			{
				var duration:int = new Date().valueOf() - startTime.valueOf();
				applicationManager.debug("$API call$","time: "+ duration,getQualifiedClassName(this)+" ("+backendMethod+")");
				
				proxyIndex--;
			};
			
			super.onServiceSuccess(event);
		}
		
		override public function onServiceFault(event:FaultEvent):void
		{
			if (isDebugMode && startTime)
			{
				applicationManager.debug("$API call$","failed",getQualifiedClassName(this)+" ("+backendMethod+")");
				proxyIndex--;
			};
			
			super.onServiceFault(event);
		}
		
	}
}